package com.atguigu.tingshu.user.mq;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
public class UserReceiver {

    @Autowired
    private UserInfoService userInfoService ;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_PAY_RECORD)
    public void saveUserPaidRecord(ConsumerRecord<String , String> consumerRecord) {
        String value = consumerRecord.value();
        if(!StringUtils.isEmpty(value)) {
            UserPaidRecordVo userPaidRecordVo = JSON.parseObject(value, UserPaidRecordVo.class);
            userInfoService.saveUserPaidRecord(userPaidRecordVo) ;
            log.info("购买信息保存成功了...orderNo: {}" , userPaidRecordVo.getOrderNo());
        }
    }

}
